﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using DungeonDelver.Engine;
using DungeonDelver.Content;
using PlayDeezGames.Common;
using DungeonDelver.Engine.Names;
using DungeonDelver.Content.Names;

namespace DungeonDelver.Client
{
    public partial class MainPage : UserControl
    {
        private ImageBrushes imageBrushes = new ImageBrushes();

        public MainPage()
        {
            InitializeComponent();
        }

        private void DrawMap(Map theMap)
        {
            ConfigurationTable terrainTable = theMap.Game.Configuration[EngineTables.Terrains];
            foreach (var column in theMap.Columns)
            {
                foreach (var row in theMap[column].Rows)
                {
                    MapCell cell = theMap[column][row];
                    string terrainIdentifier = cell.Terrain.Identifier;
                    IdentifiedDescriptor terrainDescriptor = terrainTable[terrainIdentifier];
                    StringProperty imageUri = terrainDescriptor[EngineProperties.ImageUri] as StringProperty;
                    Map[column][row][0].Fill = imageBrushes[imageUri.Value];
                }
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Maze maze = new Maze(8, 8);
            RandomNumberGenerator rng = new RandomNumberGenerator();
            maze.Generate(rng);

            DungeonDelver.Content.Factories.Register();
            XDocument document = XDocument.Load("/DungeonDelver.Content;component/game-template.xml");
            Game game = EntityFactories.Load<Game>(null,document.Root);

            Descriptor images = game.Configuration[EngineTables.PropertyGroups][PropertyGroups.Images];
            foreach (var property in images.Properties)
            {
                StringProperty theProperty = images[property] as StringProperty;
                imageBrushes.LookUpTable[property] = theProperty.Value;
            }

            Descriptor mapTemplates = game.Configuration[EngineTables.PropertyGroups][PropertyGroups.MapTemplates];

            Descriptor mapBuilder = game.Configuration[EngineTables.PropertyGroups][PropertyGroups.MapBuilder];
            int mapColumns = (mapBuilder[Properties.MapColumns] as IntProperty).Value;
            int mapRows = (mapBuilder[Properties.MapRows] as IntProperty).Value;

            MapBuilderProperty builder = mapBuilder[Properties.MapBuilder] as MapBuilderProperty;

            game.Atlas[0] = EntityFactories.Create(game.Atlas, EngineFactories.AtlasColumnFactory) as AtlasColumn;
            game.Atlas[0][0] = EntityFactories.Create(game.Atlas[0], EngineFactories.AtlasCellFactory) as AtlasCell;
            game.Atlas[0][0][0] = EntityFactories.Create(game.Atlas[0][0], EngineFactories.MapFactory) as Map;

            builder.Builder.BuildMap(game.Atlas[0][0][0], "c-o-x-o-x", mapTemplates,mapBuilder);

            DrawMap(game.Atlas[0][0][0]);
            DungeonDelver.Content.Factories.Unregister();
        }
    }
}
